<template>
  <view>
    <canvas canvas-id="VyoJBjJLgvDVqflwifsgbxBqLUgGptFR" id="VyoJBjJLgvDVqflwifsgbxBqLUgGptFR" class="charts" @touchend="tap"/>
  </view>
</template>


<script>
import uCharts from '@/uni_modules/qiun-data-charts/js_sdk/u-charts/u-charts.js';
var uChartsInstance = {};
export default {
  data() {
    return {
      cWidth: 750,
      cHeight: 500
    };
  },
  onReady() {
    //这里的 750 对应 css .charts 的 width
    this.cWidth = uni.upx2px(750);
    //这里的 500 对应 css .charts 的 height
    this.cHeight = uni.upx2px(500);
    this.getServerData();
  },
  methods: {
    getServerData() {
      //模拟从服务器获取数据时的延时
      setTimeout(() => {
        //模拟服务器返回数据，如果数据格式和标准格式不同，需自行按下面的格式拼接
        let res = {
            series: [
              {
                name: "气泡一",
                data: [[95,95,23,"标题1"],[30,55,33,"标题2"]]
              },
              {
                name: "气泡二",
                data: [[130,30,30,"标题3"],[200,90,40,"标题4"]]
              }
            ]
          };
        this.drawCharts('VyoJBjJLgvDVqflwifsgbxBqLUgGptFR', res);
      }, 500);
    },
    drawCharts(id,data){
      const ctx = uni.createCanvasContext(id, this);
      uChartsInstance[id] = new uCharts({
        type: "bubble",
        context: ctx,
        width: this.cWidth,
        height: this.cHeight,
        series: data.series,
        animation: true,
        background: "#FFFFFF",
        color: ["#1890FF","#91CB74","#FAC858","#EE6666","#73C0DE","#3CA272","#FC8452","#9A60B4","#ea7ccc"],
        padding: [15,15,0,15],
        legend: {},
        xAxis: {
          disableGrid: false,
          gridType: "dash",
          splitNumber: 5,
          boundaryGap: "justify",
          min: 0,
          max: 250
        },
        yAxis: {
          disableGrid: false,
          gridType: "dash",
          data: [
            {
              min: 0,
              max: 150
            }
          ]
        },
        extra: {
          bubble: {
            border: 2,
            opacity: 0.5
          }
        }
      });
    },
    tap(e){
      uChartsInstance[e.target.id].touchLegend(e);
      uChartsInstance[e.target.id].showToolTip(e);
    }
  }
};
</script>

<style scoped>
  .charts{
    width: 750rpx;
    height: 500rpx;
  }
</style>
